/**
 * 
 */
package com.toncen.samepms.common;

import org.apache.commons.beanutils.PropertyUtils;

import com.sky.common.clone.CloneUtils;

/**
 * 描述：一对一内存操作Action基类
 * 
 * @author tch
 */
public abstract class AbstractOneToOneMemoryAction<M extends IDomain> extends AbstractMemoryAction<M> {
	/**
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public AbstractOneToOneMemoryAction() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#delete(long)
	 */
	public void delete(long domainId) throws Exception {
		throw new UnsupportedOperationException();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#load(long)
	 */
	@SuppressWarnings("unchecked")
	public M load(long domainId) throws Exception {
		M domain = (M) CloneUtils.deepClone(getManyDomain());
		return domain;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#save(com.toncen.samepms.common.AbstractDomain)
	 */
	public void save(M domain) throws Exception {
		PropertyUtils.setProperty(domain, getOnePropertyName(), getOneDomain());// 把主表设置到从表中
		PropertyUtils.setProperty(getOneDomain(), getManyPropertyName(), domain);// 把从表设置到主表中
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#find(long)
	 */
	public M find(long domainId) throws Exception {
		return load(domainId);
	}

	@Override
	public void prepareOnDoEdit() throws Exception {
		if(!"domain".equals(getManyPropertyName())){
			Object property = PropertyUtils.getProperty(getOneDomain(), getManyPropertyName());
			if(property==null){	// TODO Auto-generated method stub
				return;
			}
		}
		super.prepareOnDoEdit();
	}
	
}
